package com.fyyg.admin.controller.user;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;
import com.fyyg.admin.controller.BaseController;
import com.fyyg.common.result.ResultInfo;
import com.fyyg.common.util.HttpTookit;
import com.fyyg.common.util.MD5;
import com.fyyg.common.util.MobileVerification;
import com.fyyg.common.util.StrUtil;
import com.fyyg.interfaces.TSysUserService;
import com.fyyg.model.TSysUser;

@Controller
@RequestMapping("/login")
public class TbusiUserLoginController extends BaseController{
	
	final Logger logger = LogManager.getLogger(TbusiUserLoginController.class);
	
	@Reference private TSysUserService userService;
	
	
	/**
	 * 登录页面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/loginpage")
	public String loginPage(HttpServletRequest request,HttpServletResponse response){
		return  "login";
	}
	
	/**
	 * 登录处理
	 * @param request
	 * @param response
	 * @return
	 */
	@SuppressWarnings("finally")
	@RequestMapping("/logindealwith")
    @ResponseBody
	public String loginDealWith(String userName, String passWord,HttpServletRequest request,HttpServletResponse response){
		TSysUser user  = new TSysUser();
		ResultInfo result = new ResultInfo();
    	result.setCode("success");
    	try {
    		if (StringUtils.isBlank(userName)) {
        		result.setCode("error");
        		result.setMessage("请输入用户名");
        	}else if (StringUtils.isBlank(passWord)) {
        		result.setCode("error");
        		result.setMessage("请输入密码");
        	}else {
        		user = userService.selectByPrimaryUserName(userName);
            	if(StrUtil.isBlank(user)) {
            		result.setCode("regist");
            		result.setMessage("该账户还未注册，请先注册");
            	}else {
            		String userPassword = user.getUserPassWord();
                	if(MD5.enCode(passWord).equals(userPassword)) {
                		request.getSession().setAttribute("UID",user.getUserId());
                		result.setCode("success");
                		result.setMessage("登录成功");
                	}else {
                		result.setCode("error");
                		result.setMessage("密码错误，请重新输入");
                	}
            	}
        	}
		} catch (Exception e) {
			logger.error(e.toString());
			logger.error("服务器异常");
		}finally {
			user=null;
			return result.ConversionJSON(result);
		}
	}

	
	/**
	 * 后台注册
	 * @param userName
	 * @param passWord
	 * @param pass
	 * @param request
	 * @param response
	 * @return
	 */
	@SuppressWarnings("finally")
	@RequestMapping("/registered")
    @ResponseBody
	public String registered(String userName, String passWord,String mobile,HttpServletRequest request,HttpServletResponse response){
		TSysUser user  = new TSysUser();
		ResultInfo result = new ResultInfo();
    	result.setCode("error");
    	try {
    		if (StringUtils.isBlank(userName)) {
        		result.setMessage("请输入用户名");
        	}else if (StringUtils.isBlank(passWord)) {
        		result.setMessage("请输入密码");
        	}else if(StringUtils.isBlank(mobile)){
	    		result.setMessage("请输入手机号码");
        	}else if(!MobileVerification.isPhone(mobile)) {
	    		result.setMessage("请输入正确的手机号码");
        	}else{
        		user = userService.selectByPrimaryUserMobile(mobile);
        		if(StrUtil.isBlank(user)) {
        			user = new TSysUser();
        			user.setUserName(userName);
        			user.setUserPassWord(MD5.enCode(passWord));
        			user.setUserMobile(mobile);
        			user.setUserNickName(mobile); // 默认使用手机号码做为用户昵称
        			user.setCreatedTime(new Date());
        			user.setLoginTime(new Date());
        			user.setLoginIp(HttpTookit.getRealIpAddr(request));
        			int status = userService.insert(user);
        			if(status>0) {
        				user=userService.selectByPrimaryUserName(userName);
        				request.getSession().setAttribute("UID", user.getUserId());
        				result.setCode("success");
        	    		result.setMessage("注册成功");
        			}
        		}else {
        			result.setCode("error");
    	    		result.setMessage("你输入的手机号码已被注册，请更换手机号码");
        		}
        	}
		} catch (Exception e) {
			e.printStackTrace();
			result.setMessage("服务器异常，请稍后再试");
			logger.error("服务器异常");
		}finally {
			user=null;
			return result.ConversionJSON(result);
		}
	}
	
	
	public static void main(String[] args) {
		String mobile ="15874715953";
		int i = Integer.valueOf(mobile); 
		System.out.println(i);
	}
	
	
}
